﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SanctusSoftware.Music.Chords
{
  public static class BracketWrapper
  {


    private static readonly Tuple<BracketTypes, string, string>[] Brackets = new Tuple<BracketTypes, string, string>[]
    {
      new Tuple<BracketTypes, string, string>(BracketTypes.Parentheses, "(", ")"),
      new Tuple<BracketTypes, string, string>(BracketTypes.Square, "[", "]"),
      new Tuple<BracketTypes, string, string>(BracketTypes.Curly, "{", "}"),
      new Tuple<BracketTypes, string, string>(BracketTypes.Angle, "⟨", "⟩"),
      new Tuple<BracketTypes, string, string>(BracketTypes.Inequality, "<", ">")
    };


    public static string Wrap(string text, BracketTypes bracketType)
    {

      if (text == null)
      {
        throw new ArgumentNullException("text");
      }

      var bracket = Brackets.Where(item => item.Item1 == bracketType).First();
      return bracket.Item2 + text + bracket.Item3;

    }

    public static string Unwrap(string text)
    {

      if (text == null)
      {
        throw new ArgumentNullException("text");
      }

      if (IsWrapped(text))
      {
        return text.Substring(1, text.Length - 2);
      }

      return text;

    }

    public static bool IsWrapped(string text)
    {
      foreach (var bracket in Brackets)
      {
        if (text.StartsWith(bracket.Item2) && text.EndsWith(bracket.Item3))
        {
          return true;
        }
      }
      return false;
    }

  }
}
